package zhangtao.bwie.com.yuekao2;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import Bean.CarBean;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class MyAdapter_Chiled extends RecyclerView.Adapter<MyAdapter_Chiled.ViewHolder>{
    private Context context;
    private List<CarBean.DataBean.ListBean> alist;
    public MyAdapter_Chiled(Context context, List<CarBean.DataBean.ListBean> list) {
        this.context = context;
        this.alist = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View chiled = View.inflate(context, R.layout.chiled_item, null);
        ViewHolder chiled_vh = new ViewHolder(chiled);
        return chiled_vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.cheiled_title.setText(alist.get(position).getTitle());
        String images = alist.get(position).getImages();
        if(images.contains("|")) {
            String[] split = images.split("[|]");
            holder.chiled_icons.setImageURI(split[0]);
        }else {
            holder.chiled_icons.setImageURI(images);
        }
        double price = alist.get(position).getPrice();
        holder.chiled_price.setText("￥"+price);
        holder.jia_jian_text.setText(alist.get(position).getNum()+"");
        holder.chiled_che.setChecked(alist.get(position).ischeck());
        holder.chiled_che.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(mCallBack2 != null) {
                    mCallBack2.OnCheckLinstener_Chiled(b,position);
                }
                if(mdel != null) {
                    mdel.onDelete(position,b);
                }
            }
        });
        holder.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = alist.get(position).getNum();
                holder.jia_jian_text.setText(++num +"");
                alist.get(position).setNum(num);
                if(mjiajian != null) {
                    mjiajian.Onadd(alist.get(position).getNum());
                }
            }
        });
        holder.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = alist.get(position).getNum();
                if(num == 1) {
                    return;
                }
                holder.jia_jian_text.setText(--num +"");
                alist.get(position).setNum(num);
                if(mjiajian != null) {
                    mjiajian.Onjian(alist.get(position).getNum());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if(alist == null) {
            return 0;
        }
        return alist.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private CheckBox chiled_che;
        private TextView cheiled_title;
        private SimpleDraweeView chiled_icons;
        private TextView chiled_price;
        private ImageView jian;
        private ImageView jia;
        private EditText jia_jian_text;
        public ViewHolder(View itemView) {
            super(itemView);
            cheiled_title = itemView.findViewById(R.id.chileds_titles);
            chiled_che = itemView.findViewById(R.id.chileds_check);
            chiled_icons = itemView.findViewById(R.id.chileds_shopimg);
            chiled_price = itemView.findViewById(R.id.chileds_price);
            jian = itemView.findViewById(R.id.jian);
            jia = itemView.findViewById(R.id.adds);
            jia_jian_text = itemView.findViewById(R.id.add_jian_edit);
        }
    }
    private allCheck mCallBack2;
    public void setCallBack(allCheck CallBack) {
        this.mCallBack2 = CallBack;
    }
    public interface allCheck {
        void OnCheckLinstener_Chiled(boolean isChecked,int chiledposition);
    }
    //加减接口
    private jiajian mjiajian;
    public void setAddjian(jiajian jiajians) {
        this.mjiajian = jiajians;
    }
    public interface jiajian {
        void Onadd(int addcount);
        void Onjian(int jiancount);
    }
    //    delete
    private ondelelte mdel;
    public void setOnDelete(ondelelte ondel) {
        this.mdel = ondel;
    }
    public interface ondelelte {
        void onDelete(int chiledpos,boolean chiled_flag);
    }
}
